package com.efast.cafe.portal.controller.statemach;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.statemach.PortalStateMachRel;
import com.efast.cafe.portal.service.statemach.IPortalStateMachRelService;

@Controller
@RequestMapping("/statemach")
public class PortalStateMachRelController extends AbstractController {

	private IPortalStateMachRelService portalStateMachRelService;

	@Autowired
	public void setPortalStateMachRelService(IPortalStateMachRelService portalStateMachRelService) {
		this.portalStateMachRelService = portalStateMachRelService;
	}

	@RequestMapping(value = "/rel/edit")
	public @ResponseBody ResultBean relEdit(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachRel obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachRelService.updateRel(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;

	}

	/**
	 * 企业操作新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	/*
	@RequestMapping(value = "/rel/adds")
	public @ResponseBody ResultBean relAdds(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachRelBean obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			// obj.setCompanyCode(SpringWebUtils.getRequestCompany());
			// obj.setCreateUser(SpringWebUtils.getRequestUser());
			String[] Handle_ids = obj.getHandle_ids().split(",");
			for (int i = 0; i < Handle_ids.length; i++) {
				PortalStateMachRel psmr = new PortalStateMachRel();
				psmr.setCompanyCode(SpringWebUtils.getRequestCompany());
				psmr.setConfId(obj.getConf_id());
				psmr.setStateId(obj.getState_id());
				psmr.setHandleId(Long.getLong(Handle_ids[i]));
				portalStateMachRelService.insertRel(psmr);
			}

		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}
	*/

	/**
	 * 企业操作新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/rel/add")
	public @ResponseBody ResultBean relAdd(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachRel obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setCompanyCode(SpringWebUtils.getRequestCompany());
			obj.setCreateUser(SpringWebUtils.getRequestUser());
			portalStateMachRelService.insertRel(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	/**
	 * 企业状态机配置删除
	 * 
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/rel/del")
	public @ResponseBody ResultBean relDel(@RequestBody PortalStateMachRel obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachRelService.deleteRel(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}
}
